import {GraphQLNonNull, GraphQLID} from "graphql";
import {mutationWithClientMutationId, fromGlobalId} from "graphql-relay";
import { Company } from '../../models';
import UserType from "../../types/UserType";

const DeleteCompanyMutation = mutationWithClientMutationId({
  name: 'deleteCompany',
  inputFields: {
    id: { type: new GraphQLNonNull(GraphQLID) },
  },
  outputFields: {
    id: {
      type: GraphQLID,
      resolve: ({ id }) => id,
    },
    viewer: {
      type: UserType,
      resolve: async ({ viewer }) => viewer,
    }
  },
  mutateAndGetPayload: async ({ id }, { viewer }) => {
    const localId = parseInt(fromGlobalId(id).id, 10);
    await Company.destroy({
      where: {
        id: localId,
      },
    });
    return { localId, viewer, id };
  }
});

export default DeleteCompanyMutation
